package com.crl.rcdp.sys.dao;

import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysLoginLog;
import com.crl.rcdp.sys.vo.SysLoginLogSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description: 登录日志dao
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:10
 */
@Repository
public class SysLoginLogDao2 extends BaseDao<SysLoginLog, SysLoginLogSearchVO> {
    /**
     * 根据条件统计总数
     */
    public int count(SysLoginLogSearchVO sysLoginLogSearchVO) {
        String sql = "SELECT COUNT(*) FROM td_sys_login_log WHERE 1=1 ";
        sql += createSQL(sysLoginLogSearchVO);
        return count(sql, sysLoginLogSearchVO);
    }

    private String createSQL(SysLoginLogSearchVO sysLoginLogSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(sysLoginLogSearchVO.getUsername())) {
            sql += " and username = :username";
        }

        if (StringUtil.isNotNullOrEmpty(sysLoginLogSearchVO.getStartDate())) {
            sql += " and login_date >=to_date(:startDate,'yyyy-mm-dd') ";
        }

        //截止日期默认当前选中的加一天
        if (StringUtil.isNotNullOrEmpty(sysLoginLogSearchVO.getEndDate())) {
            sql += " and login_date <=to_date(:endDate,'yyyy-mm-dd')+1 ";
        }

        return sql;
    }

    /**
     * 根据条件查询日志列表
     *
     * @param sysLoginLogSearchVO
     * @return
     */
    public List<SysLoginLog> list(SysLoginLogSearchVO sysLoginLogSearchVO) {
        String sql = "select * from td_sys_log where 1=1";
        sql += createSQL(sysLoginLogSearchVO);
        return list(sql, sysLoginLogSearchVO);
    }

    /**
     * 登录日志
     *
     * @param sysLoginLog 登录日志
     * @return
     */
    public int add(SysLoginLog sysLoginLog) {
        String sql = "insert into td_sys_login_log (id, auth_type,username,success, login_ip,login_date)" +
                " values (seq_td_sys_login_log.nextval,:authType, :username, :success, :loginIp,sysdate)";
        return insert(sql, sysLoginLog);
    }

}